test(client): add round-trip/known-answer tests for G1/G2 byte encodings - #347
Open
theo372001 wants to merge 1 commit into
Open
test(client): add round-trip/known-answer tests for G1/G2 byte encodings#347theo372001 wants to merge 1 commit into
theo372001 wants to merge 1 commit into
Conversation
feToBytes, g1ToBytes, g2ToBytes, and verificationKeyToContractFormat had been silently dropped from prove.ts by an unrelated prior change (the issue crackedstudio#123 artifact-prefetch rewrite), which broke every consumer that still imported them (contract.ts, useCircleFlow.ts, scripts/e2e.ts, prove.test.ts). Restore them alongside the new prefetch-based fullProve/prove, and export feToBytes/g1ToBytes/g2ToBytes so the exact wire format the contract deserializes can be pinned with tests. Add: - feToBytes known-answer tests (0, 1, and the max canonical Fp value — the BLS12-381 base field modulus - 1), always 48 bytes. - g1ToBytes length + limb placement (X in bytes 0-47, Y in 48-95). - g2ToBytes length + limb placement, pinning the Xc1||Xc0||Yc1||Yc0 order specifically (verified: a deliberate Xc1/Xc0 swap makes this test fail; reverting makes the suite pass clean again). - A round-trip test against the committed circuits/verification_key.json asserting ic.length === 4 for 3 public signals, correct byte lengths for every field, and byte-for-byte agreement with direct g1ToBytes/g2ToBytes calls on the same JSON. Also restores TREE_LEVELS/MAX_CIRCLE_SIZE to tree.ts (config.ts's re-export of them was broken the same way) and drops an unguarded top-level prefetchMembershipArtifacts() call in artifacts.ts that fired a real fetch() on any import of prove.ts, crashing under Node/tests — both were blocking prove.ts and its test suite from loading at all. Closes crackedstudio#48
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
Adds round-trip/known-answer tests for
feToBytes,g1ToBytes,g2ToBytes, andverificationKeyToContractFormatinpackages/client/src/prove.ts, and exports those helpers so they're testable.While implementing this I found that
feToBytes/g1ToBytes/g2ToBytes/verificationKeyToContractFormat(plusgenerateProof,CircuitInput,ContractProof,ContractVerificationKey) had been silently dropped fromprove.tsby an unrelated prior change (the issue #123 artifact-prefetch rewrite ince70090). That leftcontract.ts,app/src/hooks/useCircleFlow.ts,scripts/e2e.ts, and the existingprove.test.tsall importing names that no longer existed — the package couldn't typecheck or run its test file at all. I restored those exports (merged alongside the newer prefetch-basedfullProve/prove) so the package is internally consistent again and the functions this issue asks to test actually exist to test.Two more small, necessary fixes surfaced while getting the suite to actually run:
config.tsre-exportsTREE_LEVELS/MAX_CIRCLE_SIZEfromtree.ts, but they'd also been dropped fromtree.tsin an unrelated earlier merge. Restored (2 lines) sinceprove.ts'svalidateCircuitInput/generateProofdefault toTREE_LEVELS.artifacts.tshad an unguarded top-levelprefetchMembershipArtifacts()call that fires a realfetch("/circuits/membership.wasm")on any import ofprove.ts— it crashed every test run (and the whole file was already redundant withprove.ts's own lazy, memoizedgetArtifacts()). Removed the stray call.None of these three restorations touch the intended behavior of prior PRs — they just make the already-committed types/functions/tests load and run again.
Why
Closes #48
feToBytes,g1ToBytes,g2ToBytesimplement the exact wire format the contract deserializes (G1 = 96 bytes X||Y,G2 = 192 bytes Xc1||Xc0||Yc1||Yc0). This confirms reality matches that description:g1ToBytesplaces X in bytes 0-47 and Y in 48-95;g2ToBytesplaces the limbs inXc1||Xc0||Yc1||Yc0order exactly as documented (Xc1 before Xc0 — the counterintuitive part).Testing
cd circuits && npm test)cd contracts && cargo test)npm run e2e)cd app && npm run dev)Ran the client package's test suite directly (
node --import tsx --test src/*.test.tsfrompackages/client— the way thesenode:test-based test files actually execute;npm testthere is wired tovitest run, which doesn't recognizenode:test'stest()and reports 0 tests for every file in the package, pre-existing and unrelated to this PR):prove.test.ts: 29/29 passing (21 pre-existing + 8 new: 3feToBytes, 2g1ToBytes, 2g2ToBytes, 1 round-trip).prove.test.tsfailed to even load — missing exports).prove.ts(contract.test.tsx2,identity.test.tsx7,tree.test.tsx6 — all failing for reasons unconnected to this change, e.g.tree.proofOf is not a function). Same failures, same count, present before and after.Acceptance criterion verified: temporarily swapped the Xc1/Xc0 write order in
g2ToBytesand reran —not ok 28 - g2ToBytes places limbs in Xc1||Xc0||Yc1||Yc0 order(28 pass / 1 fail). Reverted the swap and reran — clean29 pass / 0 fail.